HAL UART initialization and configuration functions migration ¶
The goal of this page is to show how to migrate the initialization sequence using STM32CubeMX2:
Open STM32CubeMX2 UART configuration panel.
Configure the SW layer HAL.
Configure the main UART features.
Configure the additional UART features if needed.
Configure the UART GPIO pins.
Configure the UART DMA(s) if the application requires the UART to be used in DMA mode.
Configure the NVIC if the application requires the UART to be used in IT mode.
Generate the code.
Note
The configuration below might varies from a series to another.
HAL UART configuration panel ¶
create a new project with STM32CubeMX2, start by enabling the UART instance as Async, as shown in this example with the selected UART4, then start the initialization and the main configuration.
HAL UART main features configuration ¶
The UART mode is configured to Asynchronous.
The UART baud rate is set to 115200.
The UART word length is configured to 8-bit (including parity).
The UART parity is set to None.
The UART stop bit is configured to 1.
The UART data direction is set to Transmit and Receive.
The UART hardware flow control (RS232) is set to None.
The UART oversampling is configured to 16.
The UART one bit sampling is disabled.
The UART clock prescaler is set to DIV1.
HAL UART additional features configuration ¶
Configure the additional UART features if needed.
GPIO configuration ¶
Configure the
UART_TX
and the
UART_RX
GPIO pins.
GPIO TX configuration ¶
GPIO RX configuration ¶
DMA configuration ¶
Configure the DMA for data reception as needed.
DMA TX configuration ¶
DMA RX configuration ¶
NVIC configuration ¶
Configure the NVIC in case of an interrupt.
Generated UART initialization and de-initialization sequences ¶
The UART initialization sequence is generated in the
mx_uart4_hal_uart_init
function in
mx_uart4.c
file under
$YOUR_PROJECT_NAME$/$YOUR_PROJECT_NAME$_SW/generated/STM32Cube_CodeGen.
Related concepts:
|
Topic |
HAL1 |
HAL2 |
|---|---|---|
|
Handle declaration |
UART_HandleTypeDef huart4;
DMA_HandleTypeDef hLPDMA1_CH0;
DMA_HandleTypeDef hLPDMA1_CH1;
|
hal_uart_handle_t hUART4;
hal_dma_handle_t hLPDMA1_CH0;
hal_dma_handle_t hLPDMA1_CH1;
|
|
Initialization and configuration sequence |
static void MX_UART4_Init(void)
{
huart4.Instance = UART4;
huart4.Init.BaudRate = 115200;
huart4.Init.WordLength = UART_WORDLENGTH_8B;
huart4.Init.StopBits = UART_STOPBITS_1;
huart4.Init.Parity = UART_PARITY_NONE;
huart4.Init.Mode = UART_MODE_TX_RX;
huart4.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart4.Init.OverSampling = UART_OVERSAMPLING_16;
huart4.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart4.Init.ClockPrescaler = UART_PRESCALER_DIV1;
huart4.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(&huart4) != HAL_OK)
{
Error_Handler();
}
/* Fifo configuration */
if (HAL_UARTEx_SetTxFifoThreshold(&huart4, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
{
Error_Handler();
}
if (HAL_UARTEx_SetRxFifoThreshold(&huart4, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
{
Error_Handler();
}
if (HAL_UARTEx_EnableFifoMode(&huart4) != HAL_OK)
{
Error_Handler();
}
}
void HAL_UART_MspInit(UART_HandleTypeDef* huart)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
if(huart->Instance==UART4)
{
__HAL_RCC_UART4_CLK_ENABLE();
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_UART4;
PeriphClkInit.Uart4ClockSelection = RCC_UART4CLKSOURCE_PCLK1;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
{
Error_Handler();
}
/*GPIO*/
__HAL_RCC_GPIOA_CLK_ENABLE();
/* UART4 TX Configuration */
GPIO_InitStruct.Pin = GPIO_PIN_12;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF6_UART4;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* UART4 RX Configuration */
GPIO_InitStruct.Pin = GPIO_PIN_1;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF8_UART4;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* Enable interrupt */
HAL_NVIC_SetPriority(UART4_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(UART4_IRQn);
/* Configure the DMA TX */
__HAL_RCC_LPDMA1_CLK_ENABLE();
hLPDMA1_CH0.Instance = LPDMA1_Channel0;
hLPDMA1_CH0.Init.Request = LPDMA1_REQUEST_UART4_TX;
hLPDMA1_CH0.Init.Direction = DMA_MEMORY_TO_MEMORY;
hLPDMA1_CH0.Init.SrcInc = DMA_SINC_FIXED;
hLPDMA1_CH0.Init.DestInc = DMA_DINC_FIXED;
hLPDMA1_CH0.Init.SrcDataWidth = DMA_SRC_DATAWIDTH_BYTE;
hLPDMA1_CH0.Init.DestDataWidth = DMA_DEST_DATAWIDTH_BYTE;
hLPDMA1_CH0.Init.Priority = DMA_LOW_PRIORITY_LOW_WEIGHT;
if (HAL_DMA_Init(&hLPDMA1_CH0) != HAL_OK)
{
Error_Handler();
}
/* Link the Transmit DMA handle to the UART handle */
__HAL_LINKDMA(huart4, hdmarx, LPDMA1_Channel0);
/* Enable the interruption for LPDMA1_CH0 */
HAL_NVIC_SetPriority(LPDMA1_Channel0_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(LPDMA1_Channel0_IRQn);
/* Configure the DMA RX */
hLPDMA1_CH1.Instance = LPDMA1_Channel1;
hLPDMA1_CH1.Init.Request = LPDMA1_REQUEST_UART4_RX;
hLPDMA1_CH1.Init.Direction = DMA_MEMORY_TO_MEMORY;
hLPDMA1_CH1.Init.SrcInc = DMA_SINC_FIXED;
hLPDMA1_CH1.Init.DestInc = DMA_DINC_FIXED;
hLPDMA1_CH1.Init.SrcDataWidth = DMA_SRC_DATAWIDTH_BYTE;
hLPDMA1_CH1.Init.DestDataWidth = DMA_DEST_DATAWIDTH_BYTE;
hLPDMA1_CH1.Init.Priority = DMA_LOW_PRIORITY_LOW_WEIGHT;
if (HAL_DMA_Init(&hLPDMA1_CH1) != HAL_OK)
{
Error_Handler();
}
/* Link the Transmit DMA handle to the UART handle */
__HAL_LINKDMA(huart4, hdmatx, LPDMA1_Channel1);
HAL_NVIC_SetPriority(LPDMA1_Channel1_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(LPDMA1_Channel1_IRQn);
}
|
hal_uart_handle_t *mx_uart4_hal_uart_init(void)
{
hal_uart_config_t uart_config;
uart_config.baud_rate = 115200;
uart_config.clock_prescaler = HAL_UART_PRESCALER_DIV1;
uart_config.word_length = HAL_UART_WORD_LENGTH_8_BIT;
uart_config.stop_bit = HAL_UART_STOP_BIT_1;
uart_config.parity = HAL_UART_PARITY_NONE;
uart_config.direction = HAL_UART_DIRECTION_TX_RX;
uart_config.hw_flow_ctl = HAL_UART_HW_CONTROL_NONE;
uart_config.over_sampling = HAL_UART_OVERSAMPLING_16;
uart_config.one_bit_sampling = HAL_UART_ONE_BIT_SAMPLE_DISABLE;
/* Basic configuration */
if (HAL_UART_Init(&hUART4, HAL_UART4) != HAL_OK)
{
return NULL;
}
if (HAL_UART_SetConfig(&hUART4, &uart_config) != HAL_OK)
{
return NULL;
}
/* Fifo configuration */
if (HAL_UART_SetTxFifoThreshold(&hUART4, HAL_UART_FIFO_THRESHOLD_1_8) != HAL_OK)
{
return NULL;
}
if (HAL_UART_SetRxFifoThreshold(&hUART4, HAL_UART_FIFO_THRESHOLD_1_8) != HAL_OK)
{
return NULL;
}
if (HAL_UART_EnableFifoMode(&hUART4) != HAL_OK)
{
return NULL;
}
HAL_RCC_UART4_EnableClock();
if (HAL_RCC_UART4_SetKernelClkSource(HAL_RCC_UART4_CLK_SRC_PCLK1) != HAL_OK)
{
return NULL;
}
/* GPIO */
HAL_RCC_GPIOA_EnableClock();
hal_gpio_config_t gpio_config;
/** PA12 ---------> UART4_TX **/
gpio_config.mode = HAL_GPIO_MODE_ALTERNATE;
gpio_config.output_type = HAL_GPIO_OUTPUT_PUSHPULL;
gpio_config.pull = HAL_GPIO_PULL_NO;
gpio_config.speed = HAL_GPIO_SPEED_FREQ_LOW;
gpio_config.alternate = HAL_GPIO_AF_6;
HAL_GPIO_Init(HAL_GPIOA, HAL_GPIO_PIN_12, &gpio_config);
/** PA1 ---------> UART4_RX **/
gpio_config.mode = HAL_GPIO_MODE_ALTERNATE;
gpio_config.output_type = HAL_GPIO_OUTPUT_PUSHPULL;
gpio_config.pull = HAL_GPIO_PULL_NO;
gpio_config.speed = HAL_GPIO_SPEED_FREQ_LOW;
gpio_config.alternate = HAL_GPIO_AF_8;
HAL_GPIO_Init(HAL_GPIOA, HAL_GPIO_PIN_1, &gpio_config);
/* Enable interrupt */
HAL_CORTEX_NVIC_SetPriority(UART4_IRQn, HAL_CORTEX_NVIC_PREEMP_PRIORITY_0, HAL_CORTEX_NVIC_SUB_PRIORITY_0);
HAL_CORTEX_NVIC_EnableIRQ(UART4_IRQn);
/* Configure the DMA TX */
HAL_RCC_LPDMA1_EnableClock();
hal_dma_direct_xfer_config_t xfer_cfg_uart4_tx_dma;
xfer_cfg_uart4_tx_dma.request = HAL_LPDMA1_REQUEST_UART4_TX;
xfer_cfg_uart4_tx_dma.direction = HAL_DMA_DIRECTION_MEMORY_TO_PERIPH;
xfer_cfg_uart4_tx_dma.src_inc = HAL_DMA_SRC_ADDR_FIXED;
xfer_cfg_uart4_tx_dma.dest_inc = HAL_DMA_DEST_ADDR_FIXED;
xfer_cfg_uart4_tx_dma.src_data_width = HAL_DMA_SRC_DATA_WIDTH_BYTE;
xfer_cfg_uart4_tx_dma.dest_data_width = HAL_DMA_DEST_DATA_WIDTH_BYTE;
xfer_cfg_uart4_tx_dma.priority = HAL_DMA_PRIORITY_LOW_WEIGHT_LOW;
if (HAL_DMA_Init(&hLPDMA1_CH0, HAL_LPDMA1_CH0) != HAL_OK)
{
return NULL;
}
if (HAL_DMA_SetConfigPeriphDirectXfer(&hLPDMA1_CH0, &xfer_cfg_uart4_tx_dma) != HAL_OK)
{
return NULL;
}
/* Link the Transmit DMA handle to the UART handle */
if (HAL_UART_SetTxDMA(&hUART4, &hLPDMA1_CH0) != HAL_OK)
{
return NULL;
}
/* Enable the interruption for LPDMA1_CH0 */
HAL_CORTEX_NVIC_SetPriority(LPDMA1_CH0_IRQn, HAL_CORTEX_NVIC_PREEMP_PRIORITY_0, HAL_CORTEX_NVIC_SUB_PRIORITY_0);
HAL_CORTEX_NVIC_EnableIRQ(LPDMA1_CH0_IRQn);
/* Configure the DMA RX */
hal_dma_direct_xfer_config_t xfer_cfg_uart4_rx_dma;
xfer_cfg_uart4_rx_dma.request = HAL_LPDMA1_REQUEST_UART4_RX;
xfer_cfg_uart4_rx_dma.direction = HAL_DMA_DIRECTION_PERIPH_TO_MEMORY;
xfer_cfg_uart4_rx_dma.src_inc = HAL_DMA_SRC_ADDR_FIXED;
xfer_cfg_uart4_rx_dma.dest_inc = HAL_DMA_DEST_ADDR_FIXED;
xfer_cfg_uart4_rx_dma.src_data_width = HAL_DMA_SRC_DATA_WIDTH_BYTE;
xfer_cfg_uart4_rx_dma.dest_data_width = HAL_DMA_DEST_DATA_WIDTH_BYTE;
xfer_cfg_uart4_rx_dma.priority = HAL_DMA_PRIORITY_LOW_WEIGHT_LOW;
if (HAL_DMA_Init(&hLPDMA1_CH1, HAL_LPDMA1_CH1) != HAL_OK)
{
return NULL;
}
if (HAL_DMA_SetConfigPeriphDirectXfer(&hLPDMA1_CH1, &xfer_cfg_uart4_rx_dma) != HAL_OK)
{
return NULL;
}
/* Link the Transmit DMA handle to the PPP handle */
if (HAL_UART_SetRxDMA(&hUART4, &hLPDMA1_CH1) != HAL_OK)
{
return NULL;
}
/* Enable the interruption for LPDMA1_CH1 */
HAL_CORTEX_NVIC_SetPriority(LPDMA1_CH1_IRQn, HAL_CORTEX_NVIC_PREEMP_PRIORITY_0, HAL_CORTEX_NVIC_SUB_PRIORITY_0);
HAL_CORTEX_NVIC_EnableIRQ(LPDMA1_CH1_IRQn);
return &hUART4;
}
|
|
Deinitialization sequence |
void HAL_UART_MspDeInit(UART_HandleTypeDef* huart)
{
if(huart->Instance == UART4)
{
/* Disable UART4 interrupt */
HAL_NVIC_DisableIRQ(UART4_IRQn);
/* Disable UART4 clock */
__HAL_RCC_UART4_CLK_DISABLE();
/* Deinitialize GPIO pins for UART4 TX and RX */
HAL_GPIO_DeInit(GPIOA, GPIO_PIN_1); // TX
HAL_GPIO_DeInit(GPIOA, GPIO_PIN_12); // RX
/* De-initialize the DMA channels for RX and TX */
HAL_DMA_DeInit(huart->hdmarx);
HAL_DMA_DeInit(huart->hdmatx);
/* Disable the DMA interrupts */
HAL_NVIC_DisableIRQ(LPDMA1_Channel0_IRQn);
HAL_NVIC_DisableIRQ(LPDMA1_Channel1_IRQn);
}
}
|
void mx_uart4_hal_uart_deinit(void)
{
/* Disable interrupt */
HAL_CORTEX_NVIC_DisableIRQ(UART4_IRQn);
(void)HAL_UART_DeInit(&hUART4);
HAL_RCC_UART4_Reset();
HAL_RCC_UART4_DisableClock();
/* ### GPIO pins de-initialization ################################# */
/* Deinitialization of UART4: UART4_TX */
HAL_GPIO_DeInit(HAL_GPIOA, HAL_GPIO_PIN_12);
/* ### GPIO pins de-initialization ################################# */
/* Deinitialization of UART4: UART4_RX */
HAL_GPIO_DeInit(HAL_GPIOA, HAL_GPIO_PIN_1);
/* De-initialize the DMA channel */
HAL_DMA_DeInit(&hLPDMA1_CH0);
/* De-initialize the DMA channel */
HAL_DMA_DeInit(&hLPDMA1_CH1);
/* Disable the interruption for DMA */
HAL_CORTEX_NVIC_DisableIRQ(LPDMA1_CH0_IRQn);
/* Disable the interruption for DMA */
HAL_CORTEX_NVIC_DisableIRQ(LPDMA1_CH1_IRQn);
}
|
|
IRQ handle function |
void UART4_IRQHandler(void)
{
HAL_UART_IRQHandler(&huart4);
}
void LPDMA1_Channel0_IRQHandler(void)
{
HAL_DMA_IRQHandler(&hLPDMA1_CH0);
}
void LPDMA1_Channel1_IRQHandler(void)
{
HAL_DMA_IRQHandler(&hLPDMA1_CH1);
}
|
void UART4_IRQHandler(void)
{
HAL_UART_IRQHandler(&hUART4);
}
void LPDMA1_CH0_IRQHandler(void)
{
HAL_DMA_IRQHandler(&hLPDMA1_CH0);
}
void LPDMA1_CH1_IRQHandler(void)
{
HAL_DMA_IRQHandler(&hLPDMA1_CH1);
}
|
UART LIN Mode Initialization ¶
To create a new project with STM32CubeMX, start by enabling the UART instance as Serial. This example demonstrates the setup using UART4.
Code Generation ¶
|
Topic |
HAL1 |
HAL2 |
|---|---|---|
|
Handle declaration |
UART_HandleTypeDef huart4;
|
hal_uart_handle_t hUART4;
|
|
Initialization and configuration sequence |
static void MX_UART4_Init(void)
{
huart4.Instance = UART4;
huart4.Init.BaudRate = 115200;
huart4.Init.WordLength = UART_WORDLENGTH_8B;
huart4.Init.StopBits = UART_STOPBITS_1;
huart4.Init.Parity = UART_PARITY_NONE;
huart4.Init.Mode = UART_MODE_TX_RX;
huart4.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart4.Init.OverSampling = UART_OVERSAMPLING_16;
huart4.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart4.Init.ClockPrescaler = UART_PRESCALER_DIV1;
huart4.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_LIN_Init(&huart4, UART_LINBREAKDETECTLENGTH_10B) != HAL_OK)
{
Error_Handler();
}
}
|
hal_uart_handle_t *mx_uart4_hal_uart_init(void)
{
hal_uart_config_t uart_config;
uart_config.baud_rate = 115200;
uart_config.clock_prescaler = HAL_UART_PRESCALER_DIV1;
uart_config.word_length = HAL_UART_WORD_LENGTH_8_BIT;
uart_config.stop_bit = HAL_UART_STOP_BIT_1;
uart_config.parity = HAL_UART_PARITY_NONE;
uart_config.direction = HAL_UART_DIRECTION_TX_RX;
uart_config.hw_flow_ctl = HAL_UART_HW_CONTROL_NONE;
uart_config.over_sampling = HAL_UART_OVERSAMPLING_16;
uart_config.one_bit_sampling = HAL_UART_ONE_BIT_SAMPLE_DISABLE;
/* Basic configuration */
if (HAL_UART_Init(&hUART4, HAL_UART4) != HAL_OK)
{
return NULL;
}
if (HAL_UART_SetConfig(&hUART4, &uart_config) != HAL_OK)
{
return NULL;
}
if (HAL_UART_SetLINModeBreakDetectLength(&hUART4, HAL_UART_LIN_BREAK_DETECT_10_BIT) != HAL_OK)
{
return NULL;
}
if (HAL_UART_EnableLINMode(&hUART4) != HAL_OK)
{
return NULL;
}
}
|
UART RS485 Mode Initialization ¶
To configure RS485 communication in STM32CubeMX, begin by enabling the UART instance for serial communication mode. Then, enable the RS485 driver and adjust the necessary settings for driver enable polarity and timing parameters in the additional features configuration section.
Code Generation ¶
|
Topic |
HAL1 |
HAL2 |
|---|---|---|
|
Handle declaration |
UART_HandleTypeDef huart4;
|
hal_uart_handle_t hUART4;
|
|
Initialization and configuration sequence |
static void MX_UART4_Init(void)
{
huart4.Instance = UART4;
huart4.Init.BaudRate = 115200;
huart4.Init.WordLength = UART_WORDLENGTH_8B;
huart4.Init.StopBits = UART_STOPBITS_1;
huart4.Init.Parity = UART_PARITY_NONE;
huart4.Init.Mode = UART_MODE_TX_RX;
huart4.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart4.Init.OverSampling = UART_OVERSAMPLING_16;
huart4.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart4.Init.ClockPrescaler = UART_PRESCALER_DIV1;
huart4.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_RS485Ex_Init(&huart4, UART_DE_POLARITY_LOW, 0x10, 0x10) != HAL_OK)
{
Error_Handler();
}
}
|
hal_uart_handle_t *mx_uart4_hal_uart_init(void)
{
hal_uart_config_t uart_config;
uart_config.baud_rate = 115200;
uart_config.clock_prescaler = HAL_UART_PRESCALER_DIV1;
uart_config.word_length = HAL_UART_WORD_LENGTH_8_BIT;
uart_config.stop_bit = HAL_UART_STOP_BIT_1;
uart_config.parity = HAL_UART_PARITY_NONE;
uart_config.direction = HAL_UART_DIRECTION_TX_RX;
uart_config.hw_flow_ctl = HAL_UART_HW_CONTROL_NONE;
uart_config.over_sampling = HAL_UART_OVERSAMPLING_16;
uart_config.one_bit_sampling = HAL_UART_ONE_BIT_SAMPLE_DISABLE;
/* Basic configuration */
if (HAL_UART_Init(&hUART4, HAL_UART4) != HAL_OK)
{
return NULL;
}
if (HAL_UART_SetConfig(&hUART4, &uart_config) != HAL_OK)
{
return NULL;
}
/* RS485 driver enable configuration */
hal_uart_rs485_config_t uart_rs485_config;
uart_rs485_config.assertion_time_samples = 0x10 ;
uart_rs485_config.deassertion_time_samples =0x10;
uart_rs485_config.polarity = HAL_UART_DE_POLARITY_LOW;
if (HAL_UART_SetConfigRS485Mode(&hUART4, &rs485_config) != HAL_OK)
{
return NULL;
}
if (HAL_UART_EnableRS485Mode(&hUART4) != HAL_OK)
{
return NULL;
}
}
|
UART MultiProcessor Mode Initialization ¶
To create a new project with STM32CubeMX, start by enabling the UART instance for MultiProcessor mode. This example demonstrates the setup using UART4.
Additional Features Configuration ¶
When configuring the UART in MultiProcessor mode, additional settings are required to manage wake-up functionality from mute mode. These settings include:
Wake Up Method From Mute Mode: Choose the method for waking up from mute mode.
Wake Up Address Length: Specify the length of the wake-up address. This setting is necessary when the wake-up method selected is “Address Mark”.
Wake Up Address: Define the specific wake-up address. This is needed when the wake-up method selected is “Address Mark”.
These configurations are crucial for ensuring proper communication in MultiProcessor mode.
Code Generation ¶
|
Topic |
HAL1 |
HAL2 |
|---|---|---|
|
Handle declaration |
UART_HandleTypeDef huart4;
|
hal_uart_handle_t hUART4;
|
|
Initialization and configuration sequence |
static void MX_UART4_Init(void)
{
huart4.Instance = UART4;
huart4.Init.BaudRate = 115200;
huart4.Init.WordLength = UART_WORDLENGTH_8B;
huart4.Init.StopBits = UART_STOPBITS_1;
huart4.Init.Parity = UART_PARITY_NONE;
huart4.Init.Mode = UART_MODE_TX_RX;
huart4.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart4.Init.OverSampling = UART_OVERSAMPLING_16;
huart4.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart4.Init.ClockPrescaler = UART_PRESCALER_DIV1;
huart4.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_MultiProcessor_Init(&huart4, 0x01, UART_WAKEUPMETHOD_ADDRESSMARK) != HAL_OK)
{
Error_Handler();
}
if (HAL_MultiProcessorEx_AddressLength_Set(&huart1, UART_ADDRESS_DETECT_4B) != HAL_OK)
{
Error_Handler();
}
if (HAL_MultiProcessor_EnableMuteMode(&huart1) != HAL_OK)
{
Error_Handler();
}
}
|
hal_uart_handle_t *mx_uart4_hal_uart_init(void)
{
hal_uart_config_t uart_config;
uart_config.baud_rate = 115200;
uart_config.clock_prescaler = HAL_UART_PRESCALER_DIV1;
uart_config.word_length = HAL_UART_WORD_LENGTH_8_BIT;
uart_config.stop_bit = HAL_UART_STOP_BIT_1;
uart_config.parity = HAL_UART_PARITY_NONE;
uart_config.direction = HAL_UART_DIRECTION_TX_RX;
uart_config.hw_flow_ctl = HAL_UART_HW_CONTROL_NONE;
uart_config.over_sampling = HAL_UART_OVERSAMPLING_16;
uart_config.one_bit_sampling = HAL_UART_ONE_BIT_SAMPLE_DISABLE;
/* Basic configuration */
if (HAL_UART_Init(&hUART4, HAL_UART4) != HAL_OK)
{
return NULL;
}
if (HAL_UART_SetConfig(&hUART4, &uart_config) != HAL_OK)
{
return NULL;
}
/* MultiProcessor mode configuration */
hal_uart_multi_processor_mode_wakeup_config_t uart_multi_processor_mode_wakeup_config;
uart_multi_processor_mode_wakeup_config.wakeup_method = HAL_UART_WAKEUP_METHOD_ADDRESS_MARK;
uart_multi_processor_mode_wakeup_config.address_length = HAL_UART_ADDRESS_DETECT_4_BIT;
uart_multi_processor_mode_wakeup_config.address = 0x01;
if (HAL_UART_SetConfigMultiProcessorMode(&hUART4, &uart_multi_processor_mode_wakeup_config) != HAL_OK)
{
return NULL;
}
if (HAL_UART_EnableMultiProcessorMode(&hUART4) != HAL_OK)
{
return NULL;
}
}
|
UART Half-Duplex Mode Initialization ¶
To create a new project with STM32CubeMX, start by enabling the UART instance for Half-Duplex mode. This example demonstrates the setup using UART4.
Code Generation ¶
|
Topic |
HAL1 |
HAL2 |
|---|---|---|
|
Handle declaration |
UART_HandleTypeDef huart4;
|
hal_uart_handle_t hUART4;
|
|
Initialization and configuration sequence |
static void MX_UART4_Init(void)
{
huart4.Instance = UART4;
huart4.Init.BaudRate = 115200;
huart4.Init.WordLength = UART_WORDLENGTH_8B;
huart4.Init.StopBits = UART_STOPBITS_1;
huart4.Init.Parity = UART_PARITY_NONE;
huart4.Init.Mode = UART_MODE_TX_RX;
huart4.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart4.Init.OverSampling = UART_OVERSAMPLING_16;
huart4.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart4.Init.ClockPrescaler = UART_PRESCALER_DIV1;
huart4.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_HalfDuplex_Init(&huart4) != HAL_OK)
{
Error_Handler();
}
}
|
hal_uart_handle_t *mx_uart4_hal_uart_init(void)
{
hal_uart_config_t uart_config;
uart_config.baud_rate = 115200;
uart_config.clock_prescaler = HAL_UART_PRESCALER_DIV1;
uart_config.word_length = HAL_UART_WORD_LENGTH_8_BIT;
uart_config.stop_bit = HAL_UART_STOP_BIT_1;
uart_config.parity = HAL_UART_PARITY_NONE;
uart_config.direction = HAL_UART_DIRECTION_TX_RX;
uart_config.hw_flow_ctl = HAL_UART_HW_CONTROL_NONE;
uart_config.over_sampling = HAL_UART_OVERSAMPLING_16;
uart_config.one_bit_sampling = HAL_UART_ONE_BIT_SAMPLE_DISABLE;
/* Basic configuration */
if (HAL_UART_Init(&hUART4, HAL_UART4) != HAL_OK)
{
return NULL;
}
if (HAL_UART_SetConfig(&hUART4, &uart_config) != HAL_OK)
{
return NULL;
}
/* Enable Half-Duplex mode */
if (HAL_UART_EnableHalfDuplexMode(&hUART4) != HAL_OK)
{
return NULL;
}
}
|